package com.zhx.demo.controller;

import com.ygsoft.activiti.model.MyTask;
import com.ygsoft.activiti.service.impl.RuntimeServiceImpl;
import com.ygsoft.activiti.service.impl.TasksServiceImpl;
import com.zhx.demo.entity.Test;
import com.zhx.demo.reposirory.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试控制器
 *
 * @author zhx
 * @create 2017-11-21 15:30
 **/
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private TestRepository testRepository;

    @Autowired
    private TasksServiceImpl tasksService;

    @Autowired
    private RuntimeServiceImpl runtimeService;


    private final static String PROCCESS_KEY = "leave";

    /**
     * 开始流程
     *
     * @param test
     */
    @PostMapping("/start")
    @ResponseBody
    public Test start(@RequestBody Test test) {
        Test save = testRepository.save(test);

        Map<String, Object> variables = new HashMap<>();
        variables.put("date", test.getDate());

        String s = runtimeService.startProcessByKey(test.getUserId(), Long.toString(save.getId()), PROCCESS_KEY, variables);

        save.setInstanceId(s);

        /**
         * 获取下一步流程定义
         */
        String nextTask = tasksService.getNextTask(s);

        save.setPrewStr("审批中!");
        save.setStatus("审批:" + nextTask);

        testRepository.save(save);

        return save;
    }

    /**
     * 获取我的task
     */
    @PostMapping("/getMyTask")
    @ResponseBody
    public List<MyTask> getMyTask(@RequestParam("userId") String userId) {
        /**
         * 取权限操作
         * 此处userId为1时 取出的权限为  经理   2时取出的权限为人事  3时为董事长
         */

        List<String> roleList = getRoleByUserId(userId);

        List<MyTask> tasksByUserId = tasksService.getTasksByUserId(userId, roleList);

        return tasksByUserId;
    }


    /**
     * 审批某个节点
     */
    @PostMapping("/completeTask")
    @ResponseBody
    public boolean completeTask(@RequestParam("userId") String userId, @RequestParam("taskId") String taskId, @RequestParam("comment") String comment) {
        /**
         * 取权限操作
         * 此处userId为1时 取出的权限为  经理   2时取出的权限为人事  3时为董事长
         */
        String nameByUserId = getNameByUserId(userId);

        boolean b = tasksService.completeTask(taskId, userId, nameByUserId, comment);

        return b;
    }


    /**
     * 获取用户权限
     *
     * @param userId
     * @return
     */
    public List<String> getRoleByUserId(String userId) {
        List<String> roleList = new ArrayList<>();

        if (userId.equals("1")) {
            roleList.add("jl");
        } else if (userId.equals("2")) {
            roleList.add("rs");
        } else if (userId.equals("3")) {
            roleList.add("zjl");
        }

        return roleList;
    }

    /**
     * 获取用户的名称
     */
    public String getNameByUserId(String userId) {
        if (userId.equals("1")) {
            return "经理";
        } else if (userId.equals("2")) {
            return "人事";
        } else if (userId.equals("3")) {
            return "总经理";
        }
        return userId;
    }
}

